package com.topscomm.file.service;

import java.io.InputStream;
import java.util.Map;

import com.topscomm.basic.exception.ServiceException;
import com.topscomm.core.factory.FileStoreFactoryService;
import com.topscomm.file.FileInfoDTO;
import com.topscomm.file.FileProperties;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;

/**
 * 文件下载
 * 
 * @description:
 * @author: donghang
 * @date: 2021年3月18日下午11:20:56
 * @modify:
 */
public class FileDownloadFilter implements IFileProcessFilter {
	/**
	 * 下载文件到本地
	 * 
	 * @description:
	 * @param paraMap
	 * @param dataProcessChain
	 * @return
	 * @author: donghang
	 * @date: 2021年3月18日下午11:21:37
	 * @modify:
	 */
	@Override
	public boolean doProcess(Map<String, Object> paraMap, IFileProcessChain dataProcessChain) throws Exception {
		FileStoreFactoryService fileStoreFactoryService = (FileStoreFactoryService) SpringUtil
				.getBean("fileStoreFactoryService");
		FileProperties fileProperties = (FileProperties) SpringUtil.getBean("fileProperties");
		String fileName = ConvertUtil.convertToString(paraMap.get("filename"));
		String location = ConvertUtil.convertToString(paraMap.get("foldername"));
		String identifier = ConvertUtil.convertToString(paraMap.get("identifier"));
		StringBuilder filePath = new StringBuilder();
		// 下载文件路径
		String inputFilePath = filePath.append(fileProperties.getStorePath()).append("/").append(identifier).append("/")
				.append(fileName).toString();
		FileInfoDTO fileInfo = new FileInfoDTO();
		fileInfo.setBucketName(ConvertUtil.convertToString(paraMap.get("bucketname")));
		fileInfo.setFileDirPath(location);
		fileInfo.setFileName(fileName);
		fileInfo.setStorageType(ConvertUtil.convertToString(paraMap.get("storagetype")));
		InputStream inputStream = null;
		try {
			// 下载文件都本地
			fileStoreFactoryService.downLoad(fileInfo, inputFilePath);
		} catch (Exception e) {
			throw new ServiceException(e.getMessage());
		}
		if (dataProcessChain != null) {
			dataProcessChain.doProcess(paraMap);
		}
		return true;
	}

}
